package com.hefeng.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.hefeng.domain.WxBanner;
import com.hefeng.service.WxBannerService;
import com.hefeng.util.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.hefeng.common.log.annotation.Log;
import com.hefeng.common.log.enums.BusinessType;
import com.hefeng.common.security.annotation.RequiresPermissions;
import com.hefeng.common.core.web.controller.BaseController;
import com.hefeng.common.core.web.domain.AjaxResult;
import com.hefeng.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 轮播图信息Controller
 *
 * @author ruoyi
 * @date 2022-08-23
 */
@Transactional
@RestController
@RequestMapping("/banner")
public class WxBannerController extends BaseController {
    @Autowired
    private WxBannerService wxBannerService;

    /**
     * 查询轮播图信息列表
     */

    @RequiresPermissions("system:banner:list")
    @PostMapping("/list")
    public TableDataInfo list(WxBanner wxBanner) {
        startPage();
        List<WxBanner> list = wxBannerService.selectWxBannerList(wxBanner);
        return getDataTable(list);
    }



    /**
     * 获取轮播图信息详细信息
     */
    @RequiresPermissions("system:banner:query")
    @PostMapping(value = "/{bannerId}")
    public AjaxResult getInfo(@PathVariable("bannerId") Integer bannerId) {
        return AjaxResult.success(wxBannerService.selectWxBannerByBannerId(bannerId));
    }

    /**
     * 获取图片url
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/pictureurl", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult getUrl(@RequestParam(value = "file") MultipartFile file) {
        WxBanner picture = new WxBanner();
        AliOssUtil aliOssUtil = new AliOssUtil();//
        File fl = MultipartFileToFile(file);//将MultipartFile转换为File
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String pictureName = sdf.format(new Date()) + ".jpg";//生成图片名称
        String path = aliOssUtil.uploadFile(fl, pictureName);
        System.out.println(path);
        picture.setBannerImageUrl(path);
        return AjaxResult.success("上传成功", path);//封装json
    }

    /**
     * 将MultipartFile转换为File
     *
     * @param multiFile
     * @return
     */
    public static File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若须要防止生成的临时文件重复,能够在文件名后添加随机码

        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * 修改轮播图信息
     */
    @RequiresPermissions("system:banner:edit")
    @Log(title = "轮播图信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody WxBanner wxBanner) {
        System.out.println(wxBanner.toString());
        return toAjax(wxBannerService.updateWxBanner(wxBanner));
    }

    /**
     * 删除轮播图信息
     */
    @RequiresPermissions("system:banner:remove")
    @Log(title = "轮播图信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerId}")
    public AjaxResult remove(@PathVariable("bannerId") Integer bannerId) {
        return toAjax(wxBannerService.deleteWxBannerByBannerId(bannerId));
    }

    /**
     * 添加轮播图
     *
     * @param wxBanner
     * @return
     */
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody WxBanner wxBanner) {
        System.out.println(wxBanner.toString());
        return toAjax(wxBannerService.insertWxBanner(wxBanner));
    }


}
